﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmSalaryAllowanceInfo : Form
    {
        DialogResult _dlgResult;
        int _SalaryAllwanceID;

        SalaryAllowanceClient _wcfSalaryAllwance;
        SalaryAllowanceInfo _SalaryAllwanceInfo;

        RoleRightInfo _ListRole;
        bool isChange;
        public frmSalaryAllowanceInfo(int SalaryAllwanceID, RoleRightInfo ListRole)
        {
            InitializeComponent();
            LoadLanguage();

            _SalaryAllwanceInfo = new SalaryAllowanceInfo();
            _wcfSalaryAllwance = Service.SalaryAllowance();

            _dlgResult = DialogResult.Cancel;
            _SalaryAllwanceID = SalaryAllwanceID;
            this.KeyPreview = true;
            if (_SalaryAllwanceID > 0)
            {
                _SalaryAllwanceInfo = _wcfSalaryAllwance.SalaryAllowance_GetInfo(_SalaryAllwanceID);
                Info2Form();
            }
            _ListRole = ListRole;
        }

        private void Info2Form()
        {
            txtName.Text = _SalaryAllwanceInfo.Name.ToString();
            txtType.Text = _SalaryAllwanceInfo.Type.ToString();
            txtRate.Text = _SalaryAllwanceInfo.Rate.ToString("#,##0.0");
            txtLevel.Text = _SalaryAllwanceInfo.Level.ToString("#,##0");
            isChange = false;
        }
        private void form2Info()
        {
            _SalaryAllwanceInfo.Name = txtName.Text;
            _SalaryAllwanceInfo.Type = Convert.ToInt32(txtType.Text);
            _SalaryAllwanceInfo.Level = Convert.ToDecimal(txtLevel.Text);
            _SalaryAllwanceInfo.Rate = Convert.ToDecimal(txtRate.Text);

        }
        private void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }
        bool CheckValid()
        {
            if (CheckTextValid.IsEmpty(txtName.Text))
            {
                txtName.Focus();
                MessageBox.Show("Tên loại phụ cấp không được trống", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsInteger(txtType.Text))
            {
                txtType.Focus();
                MessageBox.Show("Mức phụ cấp theo loại phải là số nguyên", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtRate.Text))
            {
                txtRate.Focus();
                MessageBox.Show("Tỷ lệ phụ cấp không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtLevel.Text))
            {
                txtLevel.Focus();
                MessageBox.Show("Mức phụ cấp không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;

        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!_ListRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_SalaryAllwanceInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _SalaryAllwanceInfo.ID = 0;
            txtName.Text = "";
            txtType.Text = "";
            txtRate.Text = "";
            txtLevel.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }

            LevelValue();

            form2Info();
            if (_SalaryAllwanceInfo.ID > 0)//Update
            {
                if (!_ListRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (_wcfSalaryAllwance.SalaryAllowance_Exist(_SalaryAllwanceInfo.ID, txtName.Text, Convert.ToInt32(txtType.Text)))
                {
                    MessageBox.Show(UpdateUI.GetString("datontai"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (_wcfSalaryAllwance.SalaryAllowance_Update(_SalaryAllwanceInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    _dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else //insert
            {
                if (!_ListRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (_wcfSalaryAllwance.SalaryAllowance_Exist(_SalaryAllwanceInfo.ID, txtName.Text, Convert.ToInt32(txtType.Text)))
                {
                    MessageBox.Show(UpdateUI.GetString("datontai"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _SalaryAllwanceInfo.ID = _wcfSalaryAllwance.SalaryAllowance_Insert(_SalaryAllwanceInfo);
                if (_SalaryAllwanceInfo.ID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    _dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UpdateUI.GetString("canhbao"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void frmSalaryAllowanceInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F4:
                    btnSave_Click(null, null);
                    break;
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
            }
        }

        private void frmSalaryAllowanceInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            _wcfSalaryAllwance.Close();
            this.DialogResult = _dlgResult;
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void txtRate_Leave(object sender, EventArgs e)
        {
            LevelValue();
        }
        void LevelValue()
        {
            decimal tyle = 0;
            decimal mucphucap = 0;
            decimal luongtoithieu = 0;
            tyle = UISetting.GetDecimalNo(txtRate.Text);
            ParameterClient wcfParameter = Service.Parameter();
            luongtoithieu = UISetting.GetDecimalNo(wcfParameter.Parameter_GetByName("MinBasicSalary").Value.ToString());

            mucphucap = tyle * luongtoithieu;

            txtLevel.Text = mucphucap.ToString("#,##0");
        }
    }
}